<?php

Class Application_Model_DocumentModel extends Zend_Db_Table_Abstract {

    protected $_name = 'document_mgmt';
    protected $_fields = 'id,name,file,cabinet,switch,status,uploader_id,date,marketing_house_id,switch_progress_name';
    public $adapter;

    function init() {
        $this->adapter = $this->getAdapter();
    }
    function getAllDocuments($request) {
        $select = $this->adapter->select()
                ->from($this->_name, array('id','name','file','cabinet','switch','status','uploader_id','date'))
                ->joinLeft('cabinet_mgmt','cabinet_mgmt.id = cabinet', array('name as cabinet_name'))
                ->joinLeft('switch_mgmt','switch_mgmt.id = switch', array('name as switch_name'));
        $dbSelectPaginator = new Zend_Paginator_Adapter_DbSelect($select);
        $paginator = new Zend_Paginator($dbSelectPaginator);
        $paginator->setCurrentPageNumber($request->getParam('page'));
        $paginator->setPageRange(5);
        $paginator->setItemCountPerPage(5);
        return $paginator;
    }

    function save($data) {
        if (isset($data['id'])) {
            return $this->update($data, "id = $data[id]");
        } else {
            return $this->insert($data);
        }
    }

    function getDocument($field, $value) {        
        return $this->adapter->fetchRow("select $this->_fields from $this->_name where $field = '$value'");
    }
    
    function getAllDocument($field, $value) {        
        return $this->adapter->fetchAll("select $this->_fields from $this->_name where $field = '$value'");
    }

    function getAllUserSwitchDocument($switchId,$userId){
        return $this->adapter->fetchAll("select $this->_fields from $this->_name where switch = '$switchId' AND uploader_id = '$userId'");
    }
    
    function getProgressDocument($switchId,$progress){
        return $this->adapter->fetchRow("select $this->_fields from $this->_name where switch = '$switchId' AND switch_progress_name = '$progress'");
    }
}